package cn.yuhh.api.v1.declare;

import cn.yuhh.api.helper.ApiResponse;
import cn.yuhh.api.service.AbstractApiService;
import cn.yuhh.core.annotation.NotNull;
import cn.yuhh.core.domain.DeclareInfo;
import cn.yuhh.core.domain.User;
import cn.yuhh.core.type.Role;
import cn.yuhh.core.type.Status;
import com.magic.spring.mongo.Mongo;
import org.bson.types.ObjectId;
import org.springframework.stereotype.Component;

import java.util.Map;

/**
 * 审批领导审查接口
 */
@Component("checkService1.0")
@NotNull(name = "id")
public class CheckService extends AbstractApiService {

    /**
     * 1.审查经办人提交的结果
     * 2.同意/不同意
     * 同意:将结果发给经办人和受理员
     * 不同意:将结果发给经办人
     */

    @Override
    public Map service(Map request) {
        DeclareInfo declareInfo = Mongo.buildMongo().eq("_id", new ObjectId(id(request))).findOne(DeclareInfo.class);
        User manager = Mongo.buildMongo().eq("areaInfo.countyCode",declareInfo.getAreaInfo().getCountyCode()).eq("role", Role.ISSUE_HEADER).eq("status", Status.ACTIVE).findOne(User.class);
        mongoId(request).updateFirst(update -> {
            update.set("declareStatus", declareInfo.getDeclareStatus().handler(whether(request)));
            update.set("mainManagerId", manager.getId());
            update.set("updateAt", System.currentTimeMillis() / 1000);
        }, DeclareInfo.class);
        declareLog(id(request));
        return ApiResponse.success();
    }

}
